Skip to main content

NodeJS

Best practices

NodeJS's strength largely lies in its rich ecosystem of packages that augment its capabilities. When creating JobFlow Connect Packages using NodeJS and its packages, consider the following steps:

  1. Setup Your Development Environment:
    • Begin by creating a specific directory for your NodeJS script development.
    • Navigate to this directory and run npm init to initialize a new NodeJS project, which will generate a package.json file to manage your script's dependencies.
    • To add a new dependency, use the command npm install {package_name}. Replace {package_name} with the name of the desired package.
  2. Managing Script and Dependencies:
    • In your settings.json file, use the dev_id attribute to uniquely identify your script and its dependencies. This step ensures that when importing a JobFlow Connect Package with NodeJS modules, the dependencies are stored in a unique folder related to your script.
    • For example, if you're developing a script named “randomizepages.js”, set the dev_id to node_randomizepages. This setup ensures that both your script and its dependencies reside in a folder named node_randomizepages, facilitating an organized separation between different NodeJS scripts.
  3. Maintain Your Dependencies:
    • Throughout your development process, you may find the need to add or remove NodeJS packages. To do so, utilize the npm command within the script's development directory. As you make changes, npm will appropriately modify the package.json file and handle the respective NodeJS modules.
  4. Packaging Your Script for Deployment:
    • Once you're satisfied with your script and ready for deployment, adhere to the standard guidelines for packaging a JobFlow Connect module.
    • Your deployment package should contain:
      • Your main script (e.g., randomizepages.js)
      • The node_modules directory (containing all dependencies)
      • Essential configuration files: package.json, package-lock.json, and settings.json.

Script example

settings.json that accompanies script sample. Script runs as c:\Program Files\nodejs\node.exe copy.js

settings.json
{
"name": "Node script example. Expects Node runtime pre-installed.",
"dev_id": "jobflow.samplepackage",
"description": "This is sample Connect package using Node",
"script": "copy.js",
"email": "[email protected]",
"url": "http://efi.com/fieryjobflow",
"params": {
"RUNTIME": "c:\\Program Files\\nodejs\\node.exe"
}
}

copy.js
// Sample Node js package to copy source file to destination

const fs = require("fs");
const path = require("path");

var myArgs = process.argv.slice(2);

console.log(`number of arguments: ${myArgs}`);

var source = myArgs[0];
// Append file name to output folder
var target = path.join(myArgs[1], path.basename(myArgs[0]));

console.log(`source : ${source}`);
console.log(`target : ${target}`);

// Output will be created or overwritten by default.
fs.copyFileSync(source, target);
console.log(`${source} was copied to ${target}`);